package com.wolfman.wolfchat.utils;

import com.alibaba.fastjson.JSON;
import com.wolfman.wolfchat.vo.Result;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * @Title
 * @Description
 * @Author WolfMan
 * @Date 2022/1/15 0:42
 * @Email 2370032534@qq.com
 */
public class ErrorResponseUtil {

    public static void setResponse(HttpServletResponse response, String message, int errorCode) throws IOException {
        //将异常信息封装到结果集
        Result result = Result.error(message);
        //设置状态码为拒绝访问
        response.setStatus(errorCode);
        response.setContentType("application/json;charset=UTF-8");
        ServletOutputStream outputStream = response.getOutputStream();
        //将结果集写回
        outputStream.write(JSON.toJSONString(result).getBytes(StandardCharsets.UTF_8));
        //关闭输出流
        outputStream.close();
    }

}
